﻿#include "TradeDialogs.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../Settings.h"
#include "../../MirSounds/SoundManager.h"
#include "../../../Shared/Enums.h"
#include "../GameScene.h"
#include "../../MirNetwork/Network.h"
#include "../../../Shared/ClientPackets.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirNetwork;
using namespace Client::MirSounds;
namespace C = ClientPackets;

namespace Client::MirScenes::Dialogs
{

    TradeDialog::TradeDialog()
    {
        setIndex(389);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        Size tempVar(204, 152);
        setSize(&tempVar);
        Point tempVar2((Settings::ScreenWidth / 2) - getSize()->Width - 10, Settings::ScreenHeight - 350);
        setLocation(&tempVar2);
        setSort(true);

        #pragma region Buttons
        ConfirmButton = new MirButton();
        ConfirmButton->setIndex(520);
        ConfirmButton->setHoverIndex(521);
        Point tempVar3(135, 120);
        ConfirmButton->setLocation(&tempVar3);
        Size tempVar4(48, 25);
        ConfirmButton->setSize(&tempVar4);
        ConfirmButton->setLibrary(Libraries::Title);
        ConfirmButton->setParent(this);
        ConfirmButton->setPressedIndex(522);
        ConfirmButton->setSound(SoundList::ButtonA);
        ConfirmButton->Click += [&] (o, e)
        {
            ChangeLockState(!GameScene::getUser()->TradeLocked);
            C::TradeConfirm *tempVar5 = new C::TradeConfirm();
            tempVar5->Locked = GameScene::getUser()->TradeLocked;
            Network::Enqueue(tempVar5);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar5' statement was not added since tempVar5 was passed to a method or constructor. Handle memory management manually.
        };

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar6(getSize()->Width - 23, 3);
        CloseButton->setLocation(&tempVar6);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Hide();
            GameScene::Scene->GuestTradeDialog->Hide();
            TradeCancel();
        };

        #pragma endregion

        #pragma region Host labels
        NameLabel = new MirLabel();
        NameLabel->setParent(this);
        Point tempVar7(20, 10);
        NameLabel->setLocation(&tempVar7);
        Size tempVar8(150, 14);
        NameLabel->setSize(&tempVar8);
        NameLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        NameLabel->setNotControl(true);

        GoldLabel = new MirLabel();
        GoldLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        Font tempVar9(Settings::FontName, 8.0F);
        GoldLabel->setFont(&tempVar9);
        Point tempVar10(35, 123);
        GoldLabel->setLocation(&tempVar10);
        GoldLabel->setParent(this);
        Size tempVar11(90, 15);
        GoldLabel->setSize(&tempVar11);
        GoldLabel->setSound(SoundList::Gold);
        GoldLabel->Click += [&] (o, e)
        {
            if (GameScene::SelectedCell == nullptr && GameScene::Gold > 0)
            {
                MirAmountBox *amountBox = new MirAmountBox("Trade Amount:", 116, GameScene::Gold);
    
                amountBox->OKButton->Click += [&] (c, a)
                {
                        if (amountBox->Amount > 0)
                        {
                            GameScene::getUser()->TradeGoldAmount += amountBox->Amount;
                            C::TradeGold *tempVar12 = new C::TradeGold();
                            tempVar12->Amount = amountBox->Amount;
                            Network::Enqueue(tempVar12);

                            RefreshInterface();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar12' statement was not added since tempVar12 was passed to a method or constructor. Handle memory management manually.
                        }
                };
    
                amountBox->Show();
                GameScene::PickedUpGold = false;

            delete amountBox;
            }
        };
        #pragma endregion

        #pragma region Grids
        Grid = std::vector<MirItemCell*>(5 * 2);

        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 2; y++)
            {
                MirItemCell *tempVar13 = new MirItemCell();
                tempVar13->setItemSlot(2 * x + y);
                tempVar13->setGridType(MirGridType::Trade);
                tempVar13->setParent(this);
                Point tempVar14(x * 36 + 10 + x, y * 32 + 39 + y);
                tempVar13->setLocation(&tempVar14);
                Grid[2 * x + y] = tempVar13;

                delete tempVar13;
            }
        }
        #pragma endregion
    }

    void TradeDialog::ChangeLockState(bool lockState, bool cancelled)
    {
        GameScene::getUser()->TradeLocked = lockState;

        if (GameScene::getUser()->TradeLocked)
        {
            ConfirmButton->setIndex(521);
        }
        else
        {
            ConfirmButton->setIndex(520);
        }

        //if (!cancelled)
        //{
        //    //Send lock info to server
        //    Network.Enqueue(new C.TradeConfirm { Locked = lockState });
        //}
    }

    void TradeDialog::RefreshInterface()
    {
        NameLabel->setText(GameScene::getUser()->Name);
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
        GoldLabel->setText(GameScene::getUser()->TradeGoldAmount.ToString("###,###,##0"));

        GameScene::Scene->GuestTradeDialog->RefreshInterface();

        Redraw();
    }

    void TradeDialog::TradeAccept()
    {
        Point tempVar(Settings::ScreenWidth - GameScene::Scene->InventoryDialog->getSize()->Width, 0);
        GameScene::Scene->InventoryDialog->setLocation(&tempVar);
        GameScene::Scene->InventoryDialog->Show();

        RefreshInterface();

        Show();
        GameScene::Scene->GuestTradeDialog->Show();
    }

    void TradeDialog::TradeReset()
    {
        GameScene::Scene->GuestTradeDialog->TradeReset();

        for (int i = 0; i < GameScene::getUser()->Trade.size(); i++)
        {
            GameScene::getUser()->Trade[i] = nullptr;
        }

        GameScene::getUser()->TradeGoldAmount = 0;
        ChangeLockState(false, true);

        RefreshInterface();

        Hide();
        GameScene::Scene->GuestTradeDialog->Hide();
    }

    void TradeDialog::TradeCancel()
    {
        C::TradeCancel tempVar();
        Network::Enqueue(&tempVar);
    }

    MirItemCell *TradeDialog::GetCell(unsigned long long id)
    {
        for (int i = 0; i < Grid.size(); i++)
        {
            if (Grid[i]->getItem() == nullptr || Grid[i]->getItem().UniqueID != id)
            {
                continue;
            }
            return Grid[i];
        }
        return nullptr;
    }

std::vector<UserItem*> GuestTradeDialog::GuestItems(10);

    GuestTradeDialog::GuestTradeDialog()
    {
        setIndex(390);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        Size tempVar(204, 152);
        setSize(&tempVar);
        Point tempVar2((Settings::ScreenWidth / 2) + 10, Settings::ScreenHeight - 350);
        setLocation(&tempVar2);
        setSort(true);

        #pragma region Host labels
        GuestNameLabel = new MirLabel();
        GuestNameLabel->setParent(this);
        Point tempVar3(0, 10);
        GuestNameLabel->setLocation(&tempVar3);
        Size tempVar4(204, 14);
        GuestNameLabel->setSize(&tempVar4);
        GuestNameLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        GuestNameLabel->setNotControl(true);

        GuestGoldLabel = new MirLabel();
        GuestGoldLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        Font tempVar5(Settings::FontName, 8.0F);
        GuestGoldLabel->setFont(&tempVar5);
        Point tempVar6(35, 123);
        GuestGoldLabel->setLocation(&tempVar6);
        GuestGoldLabel->setParent(this);
        Size tempVar7(90, 15);
        GuestGoldLabel->setSize(&tempVar7);
        GuestGoldLabel->setSound(SoundList::Gold);
        GuestGoldLabel->setNotControl(true);
        #pragma endregion

        #pragma region Grids
        GuestGrid = std::vector<MirItemCell*>(5 * 2);

        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 2; y++)
            {
                MirItemCell *tempVar8 = new MirItemCell();
                tempVar8->setItemSlot(2 * x + y);
                tempVar8->setGridType(MirGridType::GuestTrade);
                tempVar8->setParent(this);
                Point tempVar9(x * 36 + 10 + x, y * 32 + 39 + y);
                tempVar8->setLocation(&tempVar9);
                GuestGrid[2 * x + y] = tempVar8;

                delete tempVar8;
            }
        }
        #pragma endregion
    }

    void GuestTradeDialog::RefreshInterface()
    {
        GuestNameLabel->setText(GuestName);
        GuestGoldLabel->setText(std::string::Format("{0:###,###,##0}", GuestGold));

        for (int i = 0; i < GuestItems.size(); i++)
        {
            if (GuestItems[i] == nullptr)
            {
                continue;
            }
            GameScene::Bind(GuestItems[i]);
        }

        Redraw();
    }

    void GuestTradeDialog::TradeReset()
    {
        for (int i = 0; i < GuestItems.size(); i++)
        {
            GuestItems[i] = nullptr;
        }

        GuestName = "";
        GuestGold = 0;

        Hide();
    }
}
